package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Idea;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.Page;
import com.kaishengit.util.RowMapper;

public class IdeaDao {
	DBHelp<Idea> db=new DBHelp<Idea>();
	public Idea findById(String i){
		String sql="select id,title,content,createtime,t_project_id,t_user_id from t_idea where id=?";
		return db.executeQueryForObject(sql, new IdeaRowMapper(), i);
	}
	public Long getCount(){
		String sql="select count(*) from t_idea";
		return (Long) db.executeQueryForFunction(sql);
	}
	public boolean save(Idea i){
		String sql="insert into t_idea(id,title,content,createtime,t_project_id,t_user_id) values (?,?,?,?,?,?)";
		return db.executeSQL(sql,i.getId(),i.getIdeaTitle(),i.getContent(),i.getCreateTime(),i.getProjectId(),i.getUserId());
	}
	public boolean update(String id,String title,String content){
		String sql="update t_idea set title=?,content=? where id=?";
		return db.executeSQL(sql,title,content,id);
	}
	public Page<Idea> findAll(Page<Idea> page){
		String sql="select * from t_idea limit ?,?";
		List<Idea> list=db.executeQueryForList(sql, new IdeaRowMapper(),page.getOffSet(),page.getPageSize());
		page.setResult(list);
		return page;
	}
	public boolean del(String id){
		String sql="delete from t_idea where id=?";
		return db.executeSQL(sql, id);
	}
	private class IdeaRowMapper implements RowMapper<Idea>{

		public Idea mapRow(ResultSet rs) throws SQLException {
			Idea idea=new Idea();
			idea.setId(rs.getString("id"));
			idea.setIdeaTitle(rs.getString("title"));
			idea.setContent(rs.getString("content"));
			idea.setUserId(rs.getString("t_user_id"));
			idea.setCreateTime(rs.getString("createtime"));
			idea.setProjectId(rs.getString("t_project_id"));
			
			return idea;
		}
		
	}

}
